package com.skywolf.chem.usercenter.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.skywolf.chem.common.sysconfig.pojo.vo.PageVO;
import com.skywolf.chem.common.sysconfig.response.RespData;
import com.skywolf.chem.common.sysconfig.utils.TLBeanUtils;
import com.skywolf.chem.usercenter.entity.Role;
import com.skywolf.chem.usercenter.pojo.param.RoleCreateParam;
import com.skywolf.chem.usercenter.pojo.param.RoleQueryParam;
import com.skywolf.chem.usercenter.pojo.param.RoleUpdateParam;
import com.skywolf.chem.usercenter.pojo.vo.RoleVO;
import com.skywolf.chem.usercenter.service.IRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author leo
 * @since 2019-12-02
 */
@Slf4j
@Api(value = "角色API", description = "角色API")
@RestController
@RequestMapping("/usercenter/role")
public class RoleController {

    @Autowired
    private IRoleService roleService;

    /**
     * 创建角色
     *
     * @param roleCreateParam 角色参数
     * @return RespData
     */
    @PostMapping
    @ApiOperation(value = "新增角色")
    public RespData<Long> createRole(@RequestBody @Validated RoleCreateParam roleCreateParam) {
        Long roleId = roleService.createRole(roleCreateParam);
        return RespData.success(roleId);
    }

    /**
     * 逻辑删除角色
     *
     * @param id 角色ID
     * @return RespData
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除角色")
    public RespData deleteRole(@PathVariable Long id) {
        roleService.deleteRole(id);
        return RespData.success();
    }

    /**
     * 获取角色
     *
     * @param id 角色ID
     * @return RespData
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "获取角色详情")
    public RespData<RoleVO> getRole(@PathVariable Long id) {
        Role role = roleService.getRole(id);
        RoleVO roleVO = new RoleVO();
        BeanUtils.copyProperties(role, roleVO);
        return RespData.success(roleVO);
    }

    /**
     * 修改角色
     *
     * @param roleUpdateParam 角色update参数
     * @return RespData
     */
    @PutMapping
    @ApiOperation(value = "更新角色")
    public RespData updateRole(@RequestBody @Validated RoleUpdateParam roleUpdateParam) {
        roleService.updateRole(roleUpdateParam);
        return RespData.success();
    }

    /**
     * 获取所有角色
     *
     * @return RespData
     */
    @GetMapping("/all")
    @ApiOperation(value = "获取所有角色")
    public RespData<List<RoleVO>> listAllRoles() {
        List<Role> roleList = roleService.listAllRoles();
        List<RoleVO> roleVOList = TLBeanUtils.copyList(roleList, RoleVO.class);
        return RespData.success(roleVOList);
    }

    /**
     * 获取所有角色
     *
     * @return RespData
     */
    @PostMapping("/page-list")
    @ApiOperation(value = "分页获取角色")
    public RespData<PageVO<Role>> pageList(@RequestBody RoleQueryParam roleQueryParam) {
        IPage<Role> page = roleService.pageList(roleQueryParam);
        return RespData.success(new PageVO<>(page));
    }


}
